package com.maxsalad.uc.dao;

import com.maxsalad.common.dao.BaseDao;
import com.maxsalad.uc.pojo.TPlatformEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * Created by 2014-400 on 2016-03-14.
 */
@Repository
public class PlatformDao extends BaseDao<TPlatformEntity> {

    private void baseSearchParam(TPlatformEntity platformEntity, StringBuffer hql) {
        if (platformEntity == null) {
            return;
        }
        if (platformEntity.getId() != 0) {
            hql.append(" and t.id =:id");
        }
        //平台编码
        if (StringUtils.isNotBlank(platformEntity.getPlatformCode())) {
            hql.append(" and t.platformCode =:platformCode");
        }
        //平台名称
        if (StringUtils.isNotEmpty(platformEntity.getPlatformName())) {
            hql.append(" and t.platformName like :platformName");
            platformEntity.setPlatformName("%" + platformEntity.getPlatformName() + "%");
        }
        //描述
        if (StringUtils.isNotEmpty(platformEntity.getDescription())) {
            hql.append(" and t.description like :description");
            platformEntity.setDescription("%" + platformEntity.getDescription() + "%");
        }

    }

    /**
     * 查询所有平台信息
     *
     * @param platformEntity
     * @return
     * @throws Exception
     */
    public Map queryAllPlatform(TPlatformEntity platformEntity) throws Exception {
        StringBuffer hql = new StringBuffer("from TPlatformEntity t where 1=1 ");
        this.baseSearchParam(platformEntity, hql);
        hql.append(" order by t.createTime desc ");
        return this.pageQueryByHql(hql.toString(), platformEntity);
    }

    /**
     * 查询所有平台信息(不分页)
     *
     * @param platformEntity
     * @return
     * @throws Exception
     */
    public List<TPlatformEntity> queryAllPlatformNoPage(TPlatformEntity platformEntity) throws Exception {
        StringBuffer hql = new StringBuffer("from TPlatformEntity t where 1=1 ");
        this.baseSearchParam(platformEntity, hql);
        hql.append(" order by t.createTime desc ");
        return this.findByHql(hql.toString(), platformEntity);
    }
}
